<?php
/**
 * controller file Created by PhpStorm.
 * User: wumengmeng
 * Date: 2019/12/05
 * Time: 14:09
 */

namespace Modules\Shop\Http\Controllers;

use Illuminate\Support\Facades\Request;
use Modules\Shop\Service\MaintainShopService;

class MaintainShopController extends BaseController
{
    /**
     * 列表
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {
        $arr_input['shop_id'] = shop_shop_id();
        $res = MaintainShopService::get_list($arr_input);

        echo_error($res);
        return view('shop::maintain_shop.index', $res['data']);
    }

    /**
     * 根据句条件获取服务的二级分类
     * @return false|string
     */
    public function get_maintain_list()
    {
        $input = Request::all();
        $pid = intval($input['pid']);

        $list = MaintainShopService::get_maintain_config($pid);

        return json_encode($list);
    }

    /**
     * 添加页面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function create()
    {
        //获取保养配置
        $father = MaintainShopService::get_maintain_config();

        //获取商品列表
        $arr_input['shop_id'] = shop_shop_id();
        $goods = MaintainShopService::get_goods_list($arr_input);

        $stock = [];
        foreach ($goods['data'] as $value) {
            foreach ($value['product'] as $item) {
                $stock[$item['id']] = $item['stock'];
            }
        }

        $data = [
            'father' => $father['data'],
            'goods' => $goods['data'],
            'stock' => json_encode($stock, JSON_UNESCAPED_UNICODE),
        ];

        return view('shop::maintain_shop.create', $data);
    }

    /**
     * 添加操作
     * @return array
     * @throws \Exception
     */
    public function store()
    {
        $arr_input = Request::input();
        $arr_input['shop_id'] = shop_shop_id();
        shiwu_start();
        $res = MaintainShopService::add_one($arr_input);
        shiwu_end($res);
        return $res;
    }

    /**
     * 修改页面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function edit()
    {
        $nId = Request::get('id', 0);
        $res = MaintainShopService::get_one((int)$nId);

        //获取商品列表
        $arr_input['shop_id'] = shop_shop_id();
        //$arr_input['maintain_config_id'] = $res['data']['maintain_config_id'];

        $goods = MaintainShopService::get_goods_list($arr_input);

        $stock = [];
        foreach ($goods['data'] as $value) {
            foreach ($value['product'] as $item) {
                $stock[$item['id']] = $item['stock'];
            }
        }

        $arr_data = [
            'data' => $res['data'],
            'goods' => $goods['data'],
            'stock' => json_encode($stock, JSON_UNESCAPED_UNICODE),
        ];

        return view('shop::maintain_shop.edit', $arr_data);
    }

    /**
     * 修改页面操作
     * @return array
     * @throws \Exception
     */
    public function update()
    {
        $arr_input = Request::input();

        shiwu_start();
        $res = MaintainShopService::edit_one($arr_input);
        shiwu_end($res);

        return $res;
    }


    /**
     * 更改状态
     * @return array
     */
    public function change_state()
    {
        $arr_input = Request::input();
        return MaintainShopService::change_one_state($arr_input);
    }

    /**
     * 删除
     * @return array|\Illuminate\Http\RedirectResponse
     * @throws \Exception
     */
    public function delete()
    {
        $type = (int)Request::get('type', 0);
        $n_id = (int)Request::get('id', 0);

        shiwu_start();
        $res = MaintainShopService::delete_one($type, $n_id);
        shiwu_end($res);

        return $res;
    }

    /**
     * 获取汽车品牌列表和该该保养适用的车型列表
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function maintain_vehicle()
    {
        $nId = Request::get('id', 0);
        $res = MaintainShopService::maintain_vehicle((int)$nId);

        $arr_data = [
            'maintain_shop_id' => $nId,
            'cell' => $res['data']['cell'],
            'vehicle' => json_encode($res['data']['vehicle']),
            'vehicle_model_data' => $res['data']['vehicle_model_data'],
            'vehicle_model_ids' => json_encode($res['data']['vehicle_model_ids']),
            'parent_id' => $res['data']['parent_id'] ?? 1
        ];

        return view('shop::maintain_shop.vehicle', $arr_data);
    }

    /**
     * 获取指定品牌下的汽车分类
     * @return false|string
     */
    public function get_vehicle_child()
    {
        $pId = Request::get('pid', 0);
        $res = MaintainShopService::get_vehicle_child($pId);

        return json_encode($res['vehicle']);
    }

    /**
     * 创建保养与汽车的关系
     * @return array
     * @throws \Exception
     */
    public function add_maintain_vehicle()
    {
        $arr_input = Request::input();

        shiwu_start();
        $res = MaintainShopService::add_maintain_vehicle($arr_input);
        shiwu_end($res);

        return $res;
    }
}